<?php
/**
 * 自动回复模型
 * User: Administrator
 * Date: 2016/3/18
 * Time: 16:04
 */

namespace common\models\DAO;


use yii\db\ActiveRecord;

class AutoReply extends ActiveRecord
{
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'key' => '关键字',
            'reply' => '回复内容',
            'createTime' => '添加时间',
            'adminID' => '添加人',
        ];
    }

    public function rules()
    {
        return [
            ['id', 'number'],
            [['key', 'reply',], 'required'],
            ['createTime', 'default', 'value'=>time(), 'on'=>'add'],
            ['adminID', 'default', 'value'=>\Yii::$app->session->get('ADMIN_ID'), 'on'=>'add'],
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [],
            'add' => ['key', 'reply'],
            'edit' => ['key', 'reply'],
        ];
    }
    
    public function beforeSave($insert)
    {
        if($insert){
            $this->createTime = time();
        }
        $this->adminID = \Yii::$app->session->get('ADMIN_ID');
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getOne($id){
        $id = intval($id);
        if (empty($id)) return null;
        return $this->findOne($id);
    }


    public function getAdmin(){
        return $this->hasOne(Admin::className(),['id'=>'adminID']);
    }
}